library(readstata13)
library(ggplot2)

recog <- read.dta13("data.dta")
reco.Fig2a<-recog[recog$expellee==1 & recog$t1==1,]




summarySE95 <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
                        conf.interval=.95, .drop=TRUE) {
  library(plyr)
  
  # New version of length which can handle NA's: if na.rm==T, don't count them
  length2 <- function (x, na.rm=FALSE) {
    if (na.rm) sum(!is.na(x))
    else       length(x)
  }
  
  # This does the summary. For each group's data frame, return a vector with
  # N, mean, and sd
  datac <- ddply(data, groupvars, .drop=.drop,
                 .fun = function(xx, col) {
                   c(N    = length2(xx[[col]], na.rm=na.rm),
                     mean = mean   (xx[[col]], na.rm=na.rm),
                     sd   = sd     (xx[[col]], na.rm=na.rm)
                   )
                 },
                 measurevar
  )
  
  # Rename the "mean" column    
  datac <- rename(datac, c("mean" = measurevar))
  
  datac$se <- datac$sd / sqrt(datac$N)  # Calculate standard error of the mean
  
  # Confidence interval multiplier for standard error
  # Calculate t-statistic for confidence interval: 
  # e.g., if conf.interval is .95, use .975 (above/below), and use df=N-1
  ciMult <- qt(conf.interval/2 + .5, datac$N-1)
  datac$ci <- datac$se * ciMult
  
  return(datac)
}






theme_base1 <- 
  theme_minimal(base_size=9)  + 
  theme(legend.position="none",  
        axis.text=element_text(size=25),axis.title.x=element_text(size=25),axis.title.y=element_text(size=25),
        plot.title = element_text(size=25, hjust= 0.5))


##############################################
#F30_1: Expellees suffered more than Refugees#
##############################################

tgc95bis <- summarySE95(reco.Fig2a, measurevar="donate", groupvars=c("recognition_suffer"))






ggplot(tgc95bis,aes(x=recognition_suffer, y=donate)) + 
  geom_bar(position=position_dodge(), stat="identity", width=.55) +
  geom_errorbar(aes(ymin=donate-ci, ymax=donate+ci),
                width=0, size=1.8,                   # Width of the error bars
                position=position_dodge(.9)) +  
  theme_base1 + ylab("Proportion saying `Yes'") +
  scale_x_continuous(name="",
                     limits = c(-0.7, 1.3), breaks = c(0,1), 
                     labels = c("Disagree","Agree")) +ggtitle("Donate to UNCHR?")
ggsave("Rec1DonateBin.pdf", width = 25, height = 25, units = "cm")  







tgc95bis <- summarySE95(reco.Fig2a, measurevar="amount", groupvars=c("recognition_suffer"))


ggplot(tgc95bis,aes(x=recognition_suffer, y=amount)) + 
  geom_bar(position=position_dodge(), stat="identity", width=.55) +
  geom_errorbar(aes(ymin=amount-ci, ymax=amount+ci),
                width=0, size=1.8,                   # Width of the error bars
                position=position_dodge(.9)) +  
  theme_base1 + ylab("Euros") +
  scale_x_continuous(name="",
                     limits = c(-0.7, 1.3), breaks = c(0,1), 
                     labels = c("Disagree","Agree"))  + ggtitle("Amount Donated")
ggsave("Rec1AmountBin.pdf", width = 25, height = 25, units = "cm")  








tgc95bis <- summarySE95(reco.Fig2a, measurevar="pc_att_std", groupvars=c("recognition_suffer"))







ggplot(tgc95bis,aes(x=recognition_suffer, y=pc_att_std)) + 
  geom_bar(position=position_dodge(), stat="identity", width=.55) +
  geom_errorbar(aes(ymin=pc_att_std-ci, ymax=pc_att_std+ci),
                width=0, size=1.8,                   # Width of the error bars
                position=position_dodge(.9)) +  
  theme_base1 + ylab("Range: From 0 to 1") +
  scale_x_continuous(name="",
                     limits = c(-0.7, 1.3), breaks = c(0,1), 
                     labels = c("Disagree","Agree")) + ggtitle("Attitudes")
ggsave("Rec1AttitudesBin.pdf", width = 25, height = 25, units = "cm")  





###########################
#F30_2: Recognition Abroad#
###########################

tgc95bis <- summarySE95(reco.Fig2a, measurevar="donate", groupvars=c("recognition_abroad"))






ggplot(tgc95bis,aes(x=recognition_abroad, y=donate)) + 
  geom_bar(position=position_dodge(), stat="identity", width=.55) +
  geom_errorbar(aes(ymin=donate-ci, ymax=donate+ci),
                width=0, size=1.8,                   # Width of the error bars
                position=position_dodge(.9)) +  
  theme_base1 + ylab("Proportion saying `Yes'") +
  scale_x_continuous(name="",
                     limits = c(-0.7, 1.3), breaks = c(0,1), 
                     labels = c("Disagree","Agree"))  + ggtitle("Donate to UNCHR?")
ggsave("Rec2DonateBin.pdf", width = 25, height = 25, units = "cm")  




tgc95bis <- summarySE95(reco.Fig2a, measurevar="amount", groupvars=c("recognition_abroad"))

ggplot(tgc95bis,aes(x=recognition_abroad, y=amount)) + 
  geom_bar(position=position_dodge(), stat="identity", width=.55) +
  geom_errorbar(aes(ymin=amount-ci, ymax=amount+ci),
                width=0, size=1.8,                   # Width of the error bars
                position=position_dodge(.9)) +  
  theme_base1 + ylab("Euros") +
  scale_x_continuous(name="",
                     limits = c(-0.7, 1.3), breaks = c(0,1), 
                     labels = c("Disagree","Agree")) + ggtitle("Amount Donated")
ggsave("Rec2AmountBin.pdf", width = 25, height = 25, units = "cm")  





tgc95bis <- summarySE95(reco.Fig2a, measurevar="pc_att_std", groupvars=c("recognition_abroad"))


ggplot(tgc95bis,aes(x=recognition_abroad, y=pc_att_std)) + 
  geom_bar(position=position_dodge(), stat="identity", width=.55) +
  geom_errorbar(aes(ymin=pc_att_std-ci, ymax=pc_att_std+ci),
                width=0, size=1.8,                   # Width of the error bars
                position=position_dodge(.9)) +  
  theme_base1 + ylab("Range: from 0 to 1") +
  scale_x_continuous(name="",
                     limits = c(-0.7, 1.3), breaks = c(0,1), 
                     labels = c("Disagree","Agree")) + ggtitle("Attitudes")
ggsave("Rec2AttitudesBin.pdf", width = 25, height = 25, units = "cm")  


